package main

import (
	"code.cloudfoundry.org/go-diodes"
	"log"
	"os"
	"time"
)

func main() {
	x := diodes.NewOneToOne(2, nil)
	y := diodes.NewOneToOne(2, nil)
	z := diodes.NewOneToOne(2, nil)

	// writer
	go func() {
		for i := 0; i < 2048; i++ {
			b := []int{i + 6, i + 7, i + 8}
			z.Set(diodes.GenericDataType(&b))
			time.Sleep(time.Millisecond * 200)
		}
	}()

	go func() {
		for i := 0; i < 2048; i++ {
			// Warning: Do not use i. By taking the address,
			// you would not get each value
			j := []int{i, i + 1, i + 2}
			x.Set(diodes.GenericDataType(&j))
			time.Sleep(time.Millisecond * 10)
		}
	}()
	go func() {
		for i := 0; i < 2048; i++ {
			a := []int{i + 3, i + 4, i + 5}
			y.Set(diodes.GenericDataType(&a))
			time.Sleep(time.Millisecond * 100)
		}
	}()
	// reader
	go func() {
		xp := diodes.NewPoller(x)
		yp := diodes.NewPoller(y)
		zp := diodes.NewPoller(z)
		for {
			a := xp.Next()
			b := yp.Next()
			c := zp.Next()
			log.Printf("a: %v, b: %v, c: %v\n", *(*[]int)(a), *(*[]int)(b), *(*[]int)(c))
		}
	}()
	b := make([]byte, 1)
	os.Stdin.Read(b)
}
